package system

import (
	"MoSkeleton/framework/core/consts"
	"MoSkeleton/framework/core/dto/response"
	"MoSkeleton/framework/utils"
	"MoSkeleton/global"
	"MoSkeleton/services"
	"MoSkeleton/web/pogos/request/system"
	"MoSkeleton/web/v1/http/base"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"strconv"
)

type SysDeptCtrl struct {
	base.BaseCtrl
}

func (s *SysDeptCtrl) GetCtrlTitle() string {
	msg, _ := utils.Translater.Translate(consts.TKey_TitleSysDeptMsg)
	return msg
}

// GetDeptAll
// @Tags 部门管理
// @Summary 获取部门列表
// @Security ApiKeyAuth
// @Param data query system.SysDeptPageReq true "部门信息"
// @Success 200 {object} response.Response{msg=string} "{"code": 200, msg: "操作成功"}"
// @Router /system/dept/list [get]
func (d *SysDeptCtrl) GetDeptAll(c *gin.Context) {
	deptPageReq := system.SysDeptPageReq{}
	if err := c.ShouldBind(&deptPageReq); err != nil {
		global.MoLogger.Info("参数错误", zap.Error(err))
		response.FailMsgAsI18n(c, consts.ValidatorParamsCheckFailCode, consts.TKey_ValidatorParamsCheckFailMsg)
		return
	}
	deptPageReq.ShouldBindParams(c)
	if itemsResp, err := services.ServiceGroupInst.SystemGroupInst.SysDeptServiceInst.GetDeptAll(c, &deptPageReq); err != nil {
		global.MoLogger.Info("查询部门列表失败", zap.Error(err))
		response.FailMsgAsI18n(c, consts.CommSelectFailCode, consts.TKey_DeptGetListFailMsg)
		return
	} else {
		response.SuccessAsI18n(c, consts.TKey_CommGetSuccessMsg, itemsResp)
		return
	}
}

// AddDept
// @Tags 部门管理
// @Summary 新增部门
// @Security ApiKeyAuth
// @Param data body system.SysDeptAddReq true "部门信息"
// @Success 200 {object} response.Response{msg=string} "{"code": 200, msg: "操作成功"}"
// @Router /system/dept [post]
func (d *SysDeptCtrl) AddDept(c *gin.Context) {
	addReq := system.SysDeptAddReq{}
	if err := c.ShouldBindJSON(&addReq); err != nil {
		global.MoLogger.Info("参数错误", zap.Error(err))
		response.FailMsgAsI18n(c, consts.ValidatorParamsCheckFailCode, consts.TKey_ValidatorParamsCheckFailMsg)
		return
	}
	addReq.CreatedBy = utils.GetUsernameFromGin(c)
	addReq.UpdatedBy = addReq.CreatedBy

	if isUnique, err := services.ServiceGroupInst.SystemGroupInst.SysDeptServiceInst.CheckDeptNameUnique(&addReq.DeptUniqueReq, nil); err != nil {
		global.MoLogger.Info("检测部门名称失败", zap.Error(err))
		response.FailMsgAsI18n(c, consts.CommCreatFailCode, consts.TKey_DeptCheckDeptNameFailMsg)
		return
	} else if !isUnique {
		global.MoLogger.Info("部门名称重复", zap.Error(err))
		response.FailMsgAsI18n(c, consts.CommCreatFailCode, consts.TKey_DeptNameDuplicateMsg)
		return
	}
	if ok, err := services.ServiceGroupInst.SystemGroupInst.SysDeptServiceInst.AddDept(&addReq); err != nil {
		global.MoLogger.Info("添加部门失败", zap.Error(err))
		response.FailMsgAsI18n(c, consts.CommCreatFailCode, consts.TKey_DeptAddFailMsg)
		return
	} else if !ok {
		response.FailMsgAsI18n(c, consts.CommCreatFailCode, consts.TKey_DeptAddFailMsg)
		return
	}
	response.SuccessAsI18n(c, consts.TKey_CommCreatSuccessMsg, nil)
}

// DeleteById
// @Tags 部门管理
// @Summary 删除部门
// @Security ApiKeyAuth
// @Param data path string true "部门信息"
// @Success 200 {object} response.Response{msg=string} "{"code": 200, msg: "操作成功"}"
// @Router /system/dept/{id} [delete]
func (d *SysDeptCtrl) DeleteById(c *gin.Context) {
	idStr := c.Param("id")
	id, err := strconv.ParseUint(idStr, 10, 64)
	if err != nil {
		global.MoLogger.Info("参数错误", zap.Error(err))
		response.FailMsgAsI18n(c, consts.ValidatorParamsCheckFailCode, consts.TKey_ValidatorParamsCheckFailMsg)
		return
	}
	if ok, err := services.ServiceGroupInst.SystemGroupInst.SysDeptServiceInst.DeleteById(c, id); err != nil {
		global.MoLogger.Info("删除部门失败", zap.Error(err))
		response.FailErrorWithCodeAsI18n(c, err, consts.CommDeleteFailCode, consts.TKey_DeptDelFailMsg)
		return
	} else if !ok {
		response.FailMsgAsI18n(c, consts.CommDeleteFailCode, consts.TKey_DeptDelFailMsg)
		return
	}
	response.SuccessAsI18n(c, consts.TKey_CommDeleteSuccessMsg, nil)
}

// GetDeptById
// @Tags 部门管理
// @Summary 根据id获取部门信息
// @Security ApiKeyAuth
// @Param data path string true "部门信息"
// @Success 200 {object} response.Response{msg=string} "{"code": 200, msg: "操作成功"}"
// @Router /system/dept/{id} [get]
func (d *SysDeptCtrl) GetDeptById(c *gin.Context) {
	idStr := c.Param("id")
	id, err := strconv.ParseUint(idStr, 10, 64)
	if err != nil {
		global.MoLogger.Info("参数错误", zap.Error(err))
		response.FailMsgAsI18n(c, consts.ValidatorParamsCheckFailCode, consts.TKey_ValidatorParamsCheckFailMsg)
		return
	}
	if itemResp, err := services.ServiceGroupInst.SystemGroupInst.SysDeptServiceInst.GetDeptById(c, id); err != nil {
		global.MoLogger.Info("查询部门失败", zap.Error(err))
		response.FailMsgAsI18n(c, consts.CommSelectFailCode, consts.TKey_DeptGetFailMsg)
		return
	} else {
		response.SuccessAsI18n(c, consts.TKey_CommGetSuccessMsg, itemResp)
		return
	}
}

// UpdateDeptById
// @Tags 部门管理
// @Summary 修改部门信息
// @Security ApiKeyAuth
// @Param data body system.SysDeptUpdateReq true "部门信息"
// @Success 200 {object} response.Response{msg=string} "{"code": 200, msg: "操作成功"}"
// @Router /system/dept [put]
func (d *SysDeptCtrl) UpdateDeptById(c *gin.Context) {
	updateReq := system.SysDeptUpdateReq{}
	if err := c.ShouldBindJSON(&updateReq); err != nil {
		global.MoLogger.Info("参数错误", zap.Error(err))
		response.FailMsgAsI18n(c, consts.ValidatorParamsCheckFailCode, consts.TKey_ValidatorParamsCheckFailMsg)
		return
	}
	if isUnique, err := services.ServiceGroupInst.SystemGroupInst.SysDeptServiceInst.CheckDeptNameUnique(&updateReq.DeptUniqueReq, &updateReq.IDRequest); err != nil {
		global.MoLogger.Info("检测部门名称失败", zap.Error(err))
		response.FailMsgAsI18n(c, consts.CommUpdateFailCode, consts.TKey_DeptCheckDeptNameFailMsg)
		return
	} else if !isUnique {
		global.MoLogger.Info("部门名称重复", zap.Error(err))
		response.FailMsgAsI18n(c, consts.CommUpdateFailCode, consts.TKey_DeptNameDuplicateMsg)
		return
	}
	if updateReq.ID == updateReq.ParentId {
		response.FailMsgAsI18n(c, consts.CommUpdateFailCode, consts.TKey_DeptParentIsSelfFailMsg)
		return
	}
	updateReq.UpdatedBy = utils.GetUsernameFromGin(c)
	if ok, err := services.ServiceGroupInst.SystemGroupInst.SysDeptServiceInst.UpdateDeptById(c, &updateReq); err != nil {
		global.MoLogger.Info("更新部门失败", zap.Error(err))
		response.FailErrorWithCodeAsI18n(c, err, consts.CommUpdateFailCode, consts.TKey_DeptUpdateFailMsg)
		return
	} else if !ok {
		response.FailMsgAsI18n(c, consts.CommUpdateFailCode, consts.TKey_DeptUpdateFailMsg)
		return
	}
	response.SuccessAsI18n(c, consts.TKey_CommUpdateSuccessMsg, nil)
}

// GetExcludeDeptAndChildren
// @Tags 部门管理
// @Summary 获取部门下拉树列表
// @Security ApiKeyAuth
// @Param data query string true "部门id列表"
// @Success 200 {object} response.Response{msg=string} "{"code": 200, msg: "操作成功"}"
// @Router /system/dept/list/exclude/{id} [get]
func (d *SysDeptCtrl) GetExcludeDeptAndChildren(c *gin.Context) {
	idStr := c.Param("id")
	if idStr == "" {
		response.FailMsgAsI18n(c, consts.ValidatorParamsCheckFailCode, consts.TKey_ValidatorParamsCheckFailMsg)
		return
	}
	if itemsResp, err := services.ServiceGroupInst.SystemGroupInst.SysDeptServiceInst.GetExcludeDeptAndChildren(idStr); err != nil {
		global.MoLogger.Info("查询部门失败", zap.Error(err))
		response.FailMsgAsI18n(c, consts.CommSelectFailCode, consts.TKey_DeptGetFailMsg)
		return
	} else {
		response.SuccessAsI18n(c, consts.TKey_CommGetSuccessMsg, itemsResp)
		return
	}
}
